<?php
class game{

	public $statut = 0;
	public $nb_player = 0;
	private $players = array();
	private $current_player = 0;

	//accesseurs
	function __get($name) {
    		return $this->$name;
  	}
  	function __set($name,$value) {
    		$this->$name=$value;
  	}
  	private function reset_game(){
  		$this->statut = 0;
  		$this->nb_player = 0;
  		$this->players = array();
  		$this->current_player = 0;
  	}
	public function next_step(){
		$this->statut++;	
	}
	private function next_player(){
		$this->current_player++;
	}
	private function add_player($player){	
		$this->players[] = $player;
	}
	private function remove_player($player){
		unset($his->players[$player->id]);		
	}
	public function start(){
		foreach ($this->players as $player){
			$player->game_stat["nb_coup_bu"] = 0;
			$player->game_stat["nb_coup_donne"] = 0;
			$player->game_stat["nb_coup_des"] = 0;
		}
		$this->next_step();
	}
	public function end(){
		$this->update_player_stat();
	}
	private function update_player_stat(){
		foreach ($this->players as $player){
			$player->update_stat();
		}
	}
	public function play(){
		$player = $this->players[$this->current_player];
		echo "<h3>Joueur ".($player->id+1)." : ".$player->pseudo."</h3>";
		if ($this->statut == 3){
		//Tirage des Piges
			//tirage du dés 
			$de1 = rand(1,6);
			print $de1."<br/>";

			//simple pigeon	
			if ($de1 == 3){
				$player->pigeon = 1;				
				print "t'est pigeon<br/>";			
			}
			//multi pigeon
			elseif ($de1 == 6){
				$player->pigeon = 2;
				$de1 = rand(1,6);
				print $de1."<br/>";
				while ($de1 == 6){				
					$player->pigeon ++;				
					$de1 = rand(1,6);
					print "multi pigeon ?<br/>";
					print $de1."<br/>";
				}	
				if ($de1 == 3){
					print " Youhou t'est ".$player->pigeon."X pigeon <br/>Bonne chance<br/>";
				}
				else{
					$player->pigeon = 0;
					print " Dommage<br/>";
				}
			}
			
			if ( $this->current_player == $this->nb_player-1){
				foreach ($this->players as $player){
					if ($player->pigeon > 0){
						$this->statut = 4;
						$this->current_player = 0;
					}
					else{
						$this->current_player = 0;
					}			
				}		
			}
			else{
				$this->next_player();							
			}
		}
		elseif($this->statut == 4){
		//Jeu du Pigeon
			//Tirage des dés	
			$de1 = rand(1,6);
			$de2 = rand(1,6);
			print $de1." ".$de2."<br/>";
			
			//moteur du jeu
			$player->game_stat['nb_coup_des']++;
			$action =false;
			if ($de1== 3 and $de2==3){
				$action = true;
				echo "Les pigeons boivent 2 fois plus! <br />";
				$this->faire_boire($this->get_players_id($player,"pigeon"));
				$this->faire_boire($this->get_players_id($player,"pigeon"));
			}
			else if ($de1== 3 or $de2==3){
				$action = true;
				echo "Les pigeons boivent ! <br />";
				$this->faire_boire($this->get_players_id($player,"pigeon"));
			}
			if (($de1+$de2) == 9){
				$action = true;
				echo "Let's go ! </br>";
			}
			if (($de1==5 and $de2==1) or ($de2 == 5 and $de1 == 1)){
				$action = true;
				echo "51 Tous le monde bois !!!! <br />";
				$this->faire_boire($this->get_players_id($player,"all"));
			}
			if (($de1+$de2) == 7){
				$action = true;
				$this->faire_boire($this->get_players_id($player,"droite"));
			  	echo "A droite tu bois !";
			}
			if (($de1+$de2) == 11){
				$action = true;
				$this->faire_boire($this->get_players_id($player,"gauche"));
				echo "A gauche tu bois !";
			}
			if ($de1 == $de2){
				$action = true;
				echo "Double " . $de1 . ", Tu distribus ".$de1. " coups !! <br/>";
			}
			
			if ($action == false){
				if ($player->game_stat['nb_coup_des']==1){
					echo "Rien tu bois";
					$player->bois();
				}
				else{
					echo "Rien";
				}
				if ( $this->current_player == $this->nb_player-1){		
					$this->statut = 5;		
				}
				$this->current_player++;
			}
		}
	}

	private function faire_boire($id_players){
		foreach ( $id_players as $id_player ){
			$this->players[$id_player]->bois();
			print $this->players[$id_player]->pseudo." bois"."<br/>";
		}
	}
	private function get_players_id($player,$mode){
		$return_array = array();
		switch ($mode){
			case ("droite"):
				if (($player->id - 1) < 0){
					$return_array[] = $this->nb_player - 1;
				}
				else{
					$return_array[] = $player->id - 1;
				}
				break;
			case ("gauche"):
				if (($player->id + 1) >= $this->nb_player){
					$return_array[] = 0;
				}
				else{
					$return_array[] = $player->id + 1;
				}
				break;
			case ("all"):
				foreach($this->players as $key => $player_temp){
					if ($key != $player->id){
						$return_array[] = $key;
					}
				}
				break;
			case ("pigeon"):
				foreach($this->players as $key => $player_temp){
					$i=0;
					while ($i < $player_temp->pigeon){
						$return_array[] = $key;
						$i++;
					}
				}
				break;	
		}
		$this->players[$player->id]->game_stat['nb_coup_donne'] = $this->players[$player->id]->game_stat['nb_coup_donne'] + count($return_array);
		return $return_array;		
	}
	//recuperation de données
	public function get_player_number(){
		$this->nb_player = $_POST['nbjoueur'];
		if($this->nb_player < 3){
			$this->reset_game();
		}
	}
	public function get_players(){
		$i=0;
		while ($_POST["name_j".$i]){
			$this->add_player(new player($_POST["name_j".$i],$i));
			$i++;
		}
	}
	public function get_game(){
		if ($_POST["game"] == "new"){
			$this->reset_game();
		}
		elseif ($_POST["game"] == "next"){
			$this->statut = 3;
  			$this->current_player = 0;
  			foreach ($this->players as $player){
  				$player->pigeon=-1;
  			}
		}
	}
	//affichage
	public function show_nb_players(){
		echo $this->nb_player."<br/>";
		echo count($this->players)."<br/>";
	}
	
	function save_game($name){
		if ($this->check_base_name($name) == false){
			$players_save = array();
			foreach ($this->players as $player){
				$players_save[$player->id]['pseudo']= $player->pseudo;
				$players_save[$player->id]['game_stat']= $player->game_stat;
				$players_save[$player->id]['pigeon']= $player->pigeon;
			}
				mysql_query("INSERT INTO game (name,statut,nb_player,players,current_player) VALUES ('".$name."',".$this->statut.",".$this->nb_player.",'".serialize($players_save)."',".$this->current_player.")");
		}
		else{
		//rentré le retour erreur
		}
	}
	
	function load_game($name){
		$players = array();
		$query=mysql_query("SELECT statut,nb_player,players,current_player from game where name='".$name."'") or die ('Erreur : '.mysql_error());
		while ($loadgame = mysql_fetch_array($query)){ 
			$arrayplayers = unserialize($loadgame['players']);
			$this->statut = $loadgame['statut'];
			$this->nb_player = $loadgame['nb_player'];
			$this->current_player = $loadgame['current_player'];
		}
		foreach ($arrayplayers as $key => $item){
			$player=new player($item['pseudo'],$key);
			$player->game_stat = $item['game_stat'];
			$player->pigeon = $item['pigeon'];
			$player->load_stat();
			$this->add_player($player);
		}
	}
	
	private function check_base_name($name){
		return mysql_num_rows(mysql_query("SELECT * FROM game WHERE name = '".$name."'"));	
	}
	
}
class player extends game
{
	protected $id = 0;
	protected $pseudo = "" ;
	protected $stat = array();
	protected $game_stat = array();	
	protected $pigeon = -1;
		
	//constructeur
	function __construct($pseudo,$id) {
		$this->id = $id; 		
		$this->pseudo = $pseudo;
		if ($this->check_player_name() == false){
			$this->create_player();		
		}
		$this->load_stat();
	}
	//accesseurs
  	function __get($name) {
    		return $this->$name;
  	}
  	function __set($name,$value) {
    		$this->$name=$value;
  	}
	protected function bois(){
		$this->game_stat["nb_coup_bu"]++;
	}
	private function check_player_name(){
		return mysql_num_rows(mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'"));	
	}

	private function create_player(){
		mysql_query("INSERT INTO players (pseudo) VALUES ('".$this->pseudo."')");
	}
	protected function load_stat(){
		$query = mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'");
		while ($stat = mysql_fetch_array($query)){ 		
			$this->stat["nb_partie"] = $stat['nb_partie'];
			$this->stat["nb_coup_bu"] = $stat['nb_coup_bu'];
			$this->stat["nb_coup_donne"] = $stat['nb_coup_donne'];	 	
		}
	}
	protected function update_stat(){
	//ajoute les stats de la partie en cours a la base
		$query = mysql_query("SELECT * FROM players WHERE pseudo = '".$this->pseudo."'");
		while ($stat = mysql_fetch_array($query)){ 
			$parti = $stat['nb_partie'];
			$coup_bu = $stat['nb_coup_bu'];
			$coup_donne = $stat['nb_coup_donne'];
		}
	
		mysql_query("UPDATE players SET nb_partie = ".($parti+1).",nb_coup_bu = ".($coup_bu+$this->game_stat["nb_coup_bu"]).",nb_coup_donne = ".($coup_donne+$this->game_stat["nb_coup_donne"])." WHERE pseudo like '".$this->pseudo."'")  or die ('Erreur : '.mysql_error());
			
	
	}
}
?>
